package com.platform.user.controller;

import com.platform.bean.PageInfo;
import com.platform.bean.Result;
import com.platform.user.entity.Announcement;
import com.platform.user.service.AnnouncementService;
import com.platform.utils.PrintMessageUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("announcement")
public class AnnouncementController {
    @Resource
    private AnnouncementService announcementService;

    @PostMapping("queryAnnouncementById")
    public Result<Announcement> queryAnnouncementById(@RequestBody Map<String, Object> params) {
        Announcement announcement = announcementService.queryById(Integer.parseInt(params.get("id").toString()));
        return Result.ok(announcement);
    }

    @PostMapping("topAnnouncement")
    public Result<String> topAnnouncement(@RequestBody Announcement announcement) throws IOException {
        // System.out.println(announcement.toString());
        int i = announcementService.topAnnouncement(announcement);
        if (i == 1) {
            return Result.ok(0);
        }
        return Result.ok(1);
    }

    @PostMapping("cancelTopAnnouncement")
    public Result<String> cancelTopAnnouncement(@RequestBody Announcement announcement) throws IOException {
        // System.out.println(announcement.toString());
        announcementService.cancelTopAnnouncement(announcement);
        return Result.ok("取消置顶成功");
    }

    @PostMapping("updateAnnouncement")
    public Result<String> updateAnnouncement(@RequestBody Announcement announcement) throws IOException {
        // System.out.println(announcement.toString());
        announcementService.updateAnnouncement(announcement);
        return Result.ok("添加成功");
    }

    @PostMapping("addNewAnnouncement")
    public Result<String> addNewAnnouncement(@RequestBody Announcement announcement) throws IOException {
        // System.out.println(announcement.toString());
        announcementService.addNewAnnouncement(announcement);
        return Result.ok("添加成功");
    }

    @PostMapping("queryAnnouncementPreviewByPage")
    public Result<Map<String, Object>> queryAnnouncementPreviewByPage() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", 1);
        params.put("pageSize", 10);
        params.put("ifTop", 1);
        List<Announcement> topAnnouncementList = announcementService.queryAnnouncementByPage(params).getList();
        params.remove("ifTop");
        params.put("pageSize", 3);
        params.put("type", "0");
        PrintMessageUtils.traverseMap(params);
        List<Announcement> announcementList1 = announcementService.queryAnnouncementByPage(params).getList();
        PrintMessageUtils.traverseList(announcementList1);
        params.remove("type");
        params.put("type", "1");
        List<Announcement> announcementList2 = announcementService.queryAnnouncementByPage(params).getList();
        params.remove("type");
        params.put("type", "2");
        List<Announcement> announcementList3 = announcementService.queryAnnouncementByPage(params).getList();

        Map<String, Object> res = new HashMap<>();
        res.put("topAnnouncementList", topAnnouncementList);
        res.put("announcementList1", announcementList1);
        res.put("announcementList2", announcementList2);
        res.put("announcementList3", announcementList3);
        return Result.ok(res);
    }

    @PostMapping("queryAnnouncementByPage")
    public Result<PageInfo<Announcement>> queryAnnouncementByPage(@RequestBody Map<String, Object> params) {
        PageInfo<Announcement> announcementPageInfo = announcementService.queryAnnouncementByPage(params);
        return Result.ok(announcementPageInfo);
    }

    @PostMapping("deleteAnnouncement")
    public Result deleteUser(@RequestBody Map<String, Object> params) {
        announcementService.deleteAnnouncementById(Integer.parseInt(params.get("id").toString()));
        return Result.ok("删除用户成功");
    }
}
